<?php
namespace App\Services;

/**
 * 消息分析数据服务类
 * @package App\Services
 */
class WxtjUpstream
{
    /**
     * 所有统计数据开始日期
     * @var string
     */
    private $allBeginDate;
    /**
     * 今天日期
     * @var false|string
     */
    private $todayDate;

    /**
     * 构造函数
     */
    public function __construct()
    {
        //全部拉取开始日期
        //$this->allBeginDate = '2014-12-01';
        $this->allBeginDate = date('Y').'-01-01';
        $this->todayDate = date('Y-m-d');
    }
    /**
     * 拉取所有消息发送概况数据
     * @return bool
     */
    public function pullAllMsg()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +6 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsgHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息发送概况数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsg($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-7 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageSummary($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsg::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'msg_type' => $v['msg_type'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                        'msg_count' => $v['msg_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }
    /**
     * 拉取所有消息分送分时数据
     * @return bool
     */
    public function pullAllMsgHour()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = $beginDate;
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsghourHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息分送分时数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsgHour($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-1 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageHourly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsghour::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'ref_hour' => $v['ref_hour'],
                        'msg_type' => $v['msg_type'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                        'msg_count' => $v['msg_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有
     * @return bool
     */
    public function pullAllMsgWeek()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +29 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsgweekHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息发送周数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsgWeek($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-29 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageWeekly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsgweek::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'msg_type' => $v['msg_type'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                        'msg_count' => $v['msg_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有消息发送月数据
     * @return bool
     */
    public function pullAllMsgMonth()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +29 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsgmonthHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息发送月数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsgMonth($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-29 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageMonthly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsgmonth::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'msg_type' => $v['msg_type'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                        'msg_count' => $v['msg_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有消息发送分布数据
     * @return bool
     */
    public function pullAllMsgDist()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +14 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsgdistHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息发送分布数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsgDist($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-15 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageDistSummary($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsgdist::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'count_interval' => $v['count_interval'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有消息发送分布周数据
     * @return bool
     */
    public function pullAllMsgDistWeek()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +29 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsgdistweekHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息发送分布周数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsgDistWeek($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-30 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageDistWeekly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsgdistweek::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'count_interval' => $v['count_interval'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有消息发送分布月数据
     * @return bool
     */
    public function pullAllMsgDistMonth()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +29 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUpstreamMsgdistmonthHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 消息发送分布月数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullMsgDistMonth($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-30 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->upstreamMessageDistMonthly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjUpstreamMsgdistmonth::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'count_interval' => $v['count_interval'],
                    ],
                    [
                        'msg_user' => $v['msg_user'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }
}